package com.zr.repertory.warelocation.service.impl;


import com.zr.config.ErrorMassage;
import com.zr.model.*;
import com.zr.repertory.warelocation.mapper.WareLocationMapper;
import com.zr.repertory.warelocation.service.WareLocationService;
import com.zr.util.result.ResultBuilderUtil;
import com.zr.utils.page.PageUtil;
import com.zr.utils.result.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by Administrator on 2019/11/21.
 */
@Service
public class WareLocationServiceImpl implements WareLocationService {

    @Autowired
    private WareLocationMapper wareLocationMapper;

    @Override
    public ResultUtil queryPage(WareLocationSelectVo wareLocationSelectVo) {
        PageUtil pageUtil = new PageUtil();
        //根据条件查询总条数
        int count =  wareLocationMapper.queryCount(wareLocationSelectVo);
        //根据条件查询数据
        List<WareLocationSelectVo> wareLocationSelectVoList = wareLocationMapper.queryData(wareLocationSelectVo);

        pageUtil.setPageIndex(wareLocationSelectVo.getPageIndex());
        pageUtil.setPageSize(wareLocationSelectVo.getPageSize());
        pageUtil.setTotalNumber(count);
        pageUtil.setDataList(wareLocationSelectVoList);
        return ResultBuilderUtil.success(pageUtil);
    }



    @Override
    @Transactional
    public ResultUtil update(@Valid WareLocationUpdateVo wareLocationUpdateVo) {
        if (wareLocationUpdateVo.getWareLocationCode().isEmpty()){
            return ResultBuilderUtil.error(ErrorMassage.NODATA);
        }
        return wareLocationMapper.update(wareLocationUpdateVo);

    }

    @Override
    public ResultUtil beforeUpdate(Integer wareLocationId) {
        WareLocationUpdateVo wareLocationUpdateVo = wareLocationMapper.beforeUpdate(wareLocationId);
        if (wareLocationUpdateVo==null){
            return ResultBuilderUtil.error(ErrorMassage.QUERYBYID);
        }
        return ResultBuilderUtil.success(wareLocationUpdateVo);
    }

    @Override
    public ResultUtil warehouse() {
        return ResultBuilderUtil.success(wareLocationMapper.queryWarehouseData());
    }

    @Override
    public ResultUtil queryWareArea(@Valid QueryWareAreaByWarehouseVo queryWareAreaByWarehouseVo) {
        List<WareLocationSelectVo> wareLocationSelectVoList = wareLocationMapper.queryWareAreaData(queryWareAreaByWarehouseVo);
        return ResultBuilderUtil.success(wareLocationSelectVoList);
    }



    @Override
    public ResultUtil save(@Valid WareLocationAddVo wareLocationAddVo) {
        return wareLocationMapper.save(wareLocationAddVo);
    }


}
